<template>
  <div class="login">
    <div class="loginbg"></div>
    <div class="login-panel login-pc">
      <div class="left">
        <div class="left-top" @click="login" >
          <div class="bigger">
            <img src="../../assets/image/login.png" alt="">
          </div>
        </div>
        <div class="left-bottom" @click="signup" > 
          <div class="bigger">
            <img src="../../assets/image/signup.png" alt="">
          </div>
        </div>
      </div>
      <div class="right">
        <div class="user-login" v-if="isLogin">
          <div class="logo"><h1>Seeker</h1></div>
          <!-- <form action="">
            <div class="username input-item">
              <input type="text" @keypress="login_enter($event)" placeholder="账号" required="required" v-model="user.account"><img src="../../assets/icon/user.png" alt="">
            </div>
            <div class="psw input-item">
              <input type="password" @keypress="login_enter($event)" placeholder="密码" required="required" v-model="user.psw"><img src="../../assets/icon/psw.png" alt="">

            </div>
            <el-button class="pramary login-btn" @click="login_action">登&nbsp;录</el-button>
          </form> -->
          <el-form id="loginForm" :model="ruleForm" :rules="rules" ref="ruleForm">
            <el-form-item label="账号" prop="account" class="input-item">
              <el-input v-model.number="ruleForm.account" name="account" placeholder="请输入账号" @keyup.enter.native="submitForm('ruleForm')" auto-complete="true" class=""></el-input><img src="../../assets/icon/user.png" alt="">
            </el-form-item>
            <el-form-item label="密码" prop="psw" class="input-item">
              <el-input v-model="ruleForm.psw" type="password" name="password" placeholder="请输入密码" @keyup.enter.native="submitForm('ruleForm')" @blur="origin_code_out" class=""></el-input><img src="../../assets/icon/psw.png" alt="">
            </el-form-item>
            <p class="origin-code">初始密码为123456</p>
            <el-button class="pramary login-btn" @click="submitForm('ruleForm')">登&nbsp;录</el-button>
          </el-form>
        </div>
        <div class="user-register" v-if="!isLogin">
            <div class="logo"><h1>Seeker</h1></div>
            <!-- <form action="">
              <div class="username input-item">
                <input type="text" placeholder="用户名"><img src="../../assets/icon/user.png" alt="">
              </div>
              <div class="username input-item">
                <input type="text" placeholder="账号"><img src="../../assets/icon/book.png" alt="">
              </div>
              <div class="psw input-item">
                <input type="text" placeholder="设置密码"><img src="../../assets/icon/psw.png" alt="">
              </div>
              <div class="psw input-item">
                <input type="text" placeholder="手机号"><img src="../../assets/icon/phone.png" alt="">
              </div>
              <div class="code input-item">
                <input type="text" placeholder="验证码" class="check-code"><span class="get-code" @click="getcode"></span>
              </div>
              <el-button class="pramary login-btn">注&nbsp;册</el-button>
            </form> -->
            <el-form :model="ruleForm2" id="registerForm" status-icon label-position="left" :rules="rules2" ref="ruleForm2">
              <!-- 防止谷歌自动补全 -->
              <el-form-item style="display:none">
                <el-input ></el-input>
              </el-form-item>
              <el-form-item style="display:none">
                <el-input ></el-input>
              </el-form-item>
              <el-form-item label="姓名" prop="username" class="input-item">
                <el-input v-model.number="ruleForm2.username" name="name"></el-input>
              </el-form-item>
              <el-form-item label="账号" prop="account" class="input-item">
                <el-input v-model.number="ruleForm2.account" name="account"></el-input>
              </el-form-item>
               <el-form-item label="密码" prop="psw" class="input-item">
                <el-input v-model="ruleForm2.psw" @keyup.enter.native="registerForm('ruleForm2')" type="password" name="password" autoComplete="off"></el-input>
              </el-form-item>
               <el-form-item label="再次输入" prop="psw_again" class="input-item">
                <el-input v-model="ruleForm2.psw_again" @keyup.enter.native="registerForm('ruleForm2')" type="password" autoComplete="off"></el-input>
              </el-form-item>
              <el-form-item label="" prop="check_code" class="input-item check-code">
                <el-input v-model="ruleForm2.check_code" @keyup.enter.native="registerForm('ruleForm2')"></el-input><span class="get-code" @click="getcode"></span>
              </el-form-item>
              <el-button class="pramary login-btn" @click="registerForm('ruleForm2')">注&nbsp;册</el-button>
            </el-form>
        </div>
      </div>
    </div>
    <div class="login-panel login-mobile">
        <div class="switch-m">
          <span class="switch-r" @click="signup_m">注册</span><span class="switch-l switch-active" @click="login_m">登录</span>
        </div>
        <div class="user-login" v-if="isLogin">
          <div class="logo" id="logo-m"><h1>Seeker</h1></div>
          <form action="" id="loginForm_m">
            <div class="username input-item">
              <input type="text" placeholder="学号/账号" name="account" v-model="user.account" required="required"><img src="../../assets/icon/user.png" alt="">

            </div>
            <div class="psw input-item">
              <input type="password" placeholder="初始密码:123456" name="password" v-model="user.psw" required="required"><img src="../../assets/icon/psw.png" alt="">

            </div>
            <el-button class="pramary login-btn" @click="login_action('loginForm_m')">登&nbsp;录</el-button>
            <div class="login-footer">登录表示同意该软件用户协议和隐私政策</div>
          </form>
        </div>
        <div class="user-register" v-if="!isLogin">
            <div class="logo" id="logo-m"><h1>Seeker</h1></div>
            <form action="" id="registerForm_m">
              <div class="username input-item">
                <input type="text" placeholder="用户名" name="name"><img src="../../assets/icon/user.png" alt="">
              </div>
              <div class="username input-item">
                <input type="text" placeholder="账号" id="account_m" name="account"><img src="../../assets/icon/book.png" alt="">
              </div>
              <div class="psw input-item">
                <input type="password" placeholder="设置密码" id="password_m" name="password"><img src="../../assets/icon/psw.png" alt="">
              </div>
              <div class="psw input-item">
                <input type="text" placeholder="手机号" name="phone"><img src="../../assets/icon/phone.png" alt="">
              </div>
              <div class="code input-item">
                <input type="text" placeholder="验证码" id="check_code_m" class="check-code"><p class="get-code" @click="getcode"></p>
              </div>
              <el-button class="pramary login-btn" @click="register_m">注&nbsp;册</el-button>
              <div class="login-footer">注册表示同意该软件用户协议和隐私政策</div>
            </form>
        </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "loginBox",
  props: ["logined"],
  data() {
    var checkAccount = (rule, value, callback) => {
      if (!value) {
        callback(new Error("账号不能为空"));
      } else if (!Number.isInteger(value)) {
        callback(new Error("请输入正确账号"));
      } else if (!((value + "").split("").length == 11)) {
        callback(new Error("账号为11位"));
      } else {
        callback();
      }
    };
    var checkPsw = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      } else {
        if (this.ruleForm2.psw_again !== "") {
          if (this.$refs.ruleForm2) {
            try {
              this.$refs.ruleForm2.validateField("psw_again");
            } catch (error) {
              // 无关紧要的异常
            }
          }
        }
        callback();
      }
    };
    var checkPsw_again = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (!(value == this.ruleForm2.psw)) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };
    var check_code = (rule, value, callback) => {
      let code = $('.get-code').html()
      if (!value) {
        callback(new Error('请输入验证码'))
      }else if (!(value.toLowerCase() == code.toLowerCase())) {
        callback(new Error('验证码有误'))
      }else {
        callback()
      }
    };
    var checkName = (rule, value, callback) => {
      if (!value) {
        callback(new Error("姓名不能为空"));
      } else {
        callback();
      }
    };
    return {
      isLogin: true,
      user: {
        account: "",
        psw: "",
        username: ""
      },
      ruleForm: {
        account: "",
        psw: ""
      },
      ruleForm2: {
        username: "",
        account: "",
        psw: "",
        psw_again: "",
        check_code: ""
      },
      rules: {
        account: [
          { validator: checkAccount, trigger: "blur" }
        ],
        psw: [
          { validator: checkPsw, trigger: "blur" },
          { min: 6, max: 12, message: "密码为6-12个字符", trigger: "blur" }
        ]
      },
      rules2: {
        username: [
          { validator: checkName, trigger: "blur" },
          { max: 6, message: "姓名少于六个字符", trigger: "blur" }
        ],
        account: [{ validator: checkAccount, trigger: "blur" }],
        psw: [
          { validator: checkPsw, trigger: "blur" },
          { min: 6, max: 12, message: "密码为6-12个字符", trigger: "blur" }
        ],
        psw_again: [{ validator: checkPsw_again, trigger: "blur" }],
        check_code: [{ validator: check_code, trigger: "blur" }]
      }
    };
  },
  methods: {
    login: function() {
      this.isLogin = true;
    },
    signup: function() {
      this.isLogin = false;
      // 获取验证码
      setTimeout(() => {
        this.getcode();
      }, 0);
    },
    origin_code_out: function () {  
      $('.origin-code').hide()
    },
    login_m: function() {
      this.isLogin = true;
      $(".switch-m span").removeClass("switch-active");
      $(".switch-l").addClass("switch-active");
    },
    signup_m: function() {
      this.isLogin = false;
      $(".switch-m span").removeClass("switch-active");
      $(".switch-r").addClass("switch-active");
      // 获取验证码
      setTimeout(() => {
        this.getcode();
      }, 0);
    },
    getcode: function() {
      //首先默认code为空字符串
      let code = "";
      //设置长度，这里看需求，我这里设置了4
      var codeLength = 4;
      //设置随机字符
      var random = new Array(
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z"
      );
      //循环codeLength 我设置的4就是循环4次
      for (var i = 0; i < codeLength; i++) {
        //设置随机数范围,这设置为0 ~ 36
        var index = Math.floor(Math.random() * 36);
        //字符串拼接 将每次随机的字符 进行拼接
        code += random[index];
      }
      //将拼接好的字符串赋值给展示的Value
      $(".get-code").html(code);
    },
    login_action: function(formName) {
      // this.$store.state.user = 'hahaha'
      // 登录请求
      var self = this
      $.ajax({
        type: "post",
        data: $('#'+formName).serialize(),
        url: `${ self.$store.state.domain }/user/login`,
        success: function(data) {
          if (data.meta.errorCode == "1101") {
            if (formName == 'loginForm_m') {
              // 移动端报错
              self.$message({
                type: "error",
                message: '账号或密码错误~'
              });
            }else{
              self.$alert("账号或密码错误~", "Warning", {
                confirmButtonText: "确定",
                callback: action => {
                  // $("input").val("");
                }
              })
            }
            
          } else {
            self.user.username = data.data.name;
            self.user.token = data.data.token;
            self.$store.commit("login", self.user);
            // self.$store.state.user = self.user
            // 登录成功后
            localStorage.setItem('user',JSON.stringify(self.user))
            self.$message({
              type: "success",
              message: "欢迎回来！" + data.data.name
            });
          }
        }
        // fail: function () {
        //   alert('有问题')
        // }
      });
    },
    register_action:function (formName) {
      var self = this
      $.ajax({
        type:'post',
        dataType:'json',
        url:`${ self.$store.state.domain }/user/register`,
        data: $('#'+formName).serialize(),
        success:function (data) {
          if (data.meta.errorCode == '1101') {
              self.$alert(data.meta.errorMsg, '提示', {
                confirmButtonText: '确定',
              });
              return 
          }
          if (formName == 'registerForm_m') {
            alert('注册成功~')
            // location.reload()
            self.login_action(formName)
          }else {
            self.$alert('注册成功~', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                // location.reload()
                self.login_action(formName)
              }
            });
          }
        },
        error:function () {
           alert("异常！")
        }
      })
    },
    submitForm: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login_action('loginForm');
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    registerForm: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.register_action('registerForm')
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 注册信息check
    register_m: function () {
      //账号
      let account_m = $('#account_m').val()
      if (account_m == '') {
        this.$message({
          message: '请输入账号~',
          type: 'warning'
        });
        return
      }else if (!((account_m+'').split("").length == 11)) {
        this.$message.error('账号为11位~')
        return ;
      }
      //密码
      let password_m = $('#password_m').val()
      if (password_m == '') {
        this.$message({
          message: '请输入密码~',
          type: 'warning'
        });
        return
      }else if ( ((password_m+'').split("").length < 6) || ((password_m+'').split("").length > 11) ) {
        this.$message.error('密码在6~11位')
        return ;
      }
      //验证码  
      let code = $('.get-code').html()
      if ($('#check_code_m').val().toUpperCase() == code.toUpperCase()) {
        this.register_action('registerForm_m')
      }else {
        this.$message.error('验证码输入错误~');
      }
    }
  },
  mounted: function() {
    this.getcode();
  },
  destroyed: function() {}
};
</script>

<style>
.el-input {
  display: inline !important;
}
.login-mobile {
  display: none;
}
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.loginbg {
  position: fixed;
  top: -5vh;
  bottom: 0;
  width: 105vw;
  height: 105vh;
  background: url(../../assets/image/loginbg.jpg) no-repeat;
  background-size: 100% 100%;
  filter: blur(2px);
}
.login-panel {
  color: #afafaf;
  height: 60vh;
  width: 45vw;
  min-width: 700px;
  overflow: hidden;
  position: relative;
  z-index: 999;
}
.login-panel .left {
  float: left;
  width: 50%;
  height: 100%;
}
.login-panel .left .bigger {
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.login-panel .left img {
  width: 90px;
}
.login-panel .left-top img {
  position: relative;
  top: 3vh;
  left: -3.5vh;
}
.login-panel .left-bottom img {
  position: relative;
  top: -3vh;
}
.login-panel .left .bigger:hover {
  transform: scale(1.2);
  cursor: pointer;
}
.login-panel .right {
  float: left;
  width: 50%;
  height: 100%;
  background: #f1f1f1;
}
.login-panel .left-top {
  background: rgba(255, 255, 255, 0.9);
  height: 50%;
  transition: all 0.5s;
}
.login-panel .left-top:hover,
.login-panel .left-bottom:hover {
  background: rgba(122, 153, 245, 0.9);
}
.login-panel .left-bottom {
  height: 50%;
  background: rgba(202, 102, 102, 0.85);
  transition: all 0.5s;
}
.login-panel .user-login div {
  /* margin: 0px auto; */
}
.login-panel .user-login .el-form-item {
  margin: 20px auto;
}
.login-panel .user-login .el-form-item__label {
  display: none;
}
.login-panel .user-register div {
  /* margin: 10px auto; */
}
.login-panel .user-register .el-form-item {
  margin: 15px auto;
}
.origin-code {
  font-size:12px;
}
.user-register .el-form-item__content {
  left: 60px;
}
.user-register .el-form-item__label {
  position: absolute;
}
.user-register .el-input__prefix,
.user-register .el-input__suffix {
  top: -14px;
}
.user-register .el-form-item__error {
  /* display: none; */
  left: -60px;
}
.user-register .check-code .el-form-item__content {
  left: 0px;
}
.user-register .check-code .el-form-item__error {
  left: 0;
}
.login-panel input {
  outline: none;
  padding: 10px;
  border: none;
  letter-spacing: 1px;
}
.input-item {
  width: 60%;
  border-bottom: 1px solid #c2b6b6;
  text-align: left;
}
.input-item input {
  width: 77%;
  background: none;
  text-indent: 0px;
}
.input-item img {
  width: 16px;
  position: absolute;
  top: 32%;
  right: 7%;
}
.login-panel .login-btn {
  margin: 4vh;
  width: 40%;
  transition: background 0.4s;
}
.user-register .login-btn {
  margin: 2vh;
  text-align: center;
  transition: background 0.4s;
}
.login-btn:hover {
  background: rgba(122, 153, 245, 0.9);
  color: white;
}
.login-panel .user-login .logo {
  color: #b34f4f;
  font-size: 20px;
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
    "Lucida Sans", Arial, sans-serif;
  margin-top: 11vh;
  margin-bottom: 5vh;
}
.login-panel .user-register .logo {
  color: #b34f4f;
  font-size: 20px;
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
    "Lucida Sans", Arial, sans-serif;
  margin-top: 3vh;
}
.code {
  height: 5.5vh;
  position: relative;
}
.get-code {
  background: #658de4;
  color: white;
  padding: 0px 10px;
  letter-spacing: 3px;
  cursor: pointer;
  position: absolute;
  right: 10px;
  top: 0;
  line-height: 33px;
}

@media screen and (max-width: 700px) {
  body {
    overflow-y: scroll !important;
  }
  .login-mobile {
    display: block;
    min-width: 0px;
    width: 100vw;
    position: absolute;
    top: 30vh;
    height: 70vh;
  }
  .loginbg {
    background: url(../../assets/image/loginbg-m.png) no-repeat;
    background-size: 100% 100%;
    filter: blur();
    /* display: none; */
  }
  .login-pc {
    display: none;
  }
  .input-item img {
    width: 16px;
    position: relative;
    top: 3px;
    right: 0px;
  }
  .login-panel .user-login div {
    margin: 20px auto;
  }
  #logo-m {
    color: white;
    position: fixed;
    top: 8vh;
    left: 0vw;
    margin: 0 auto;
    width: 100vw;
  }
  .login-panel .user-register div {
    margin: 3vh auto;
  }
  .input-item {
    width: 70vw;
    border: 1px solid #7b99da;
  }
  .input-item input {
    width: 82%;
    letter-spacing: 1px;
  }
  .switch-m {
    /* border-bottom: 1px solid gray; */
  }
  .switch-m span {
    display: inline-block;
    padding: 1vh 0 2vh 0;
    text-align: center;
    width: 49vw;
    border-bottom: 1px solid #c4c1c1;
  }
  .switch-active {
    color: #658de4;
    border-bottom: 1px solid #658de4 !important;
    cursor: pointer;
  }
  .user-register {
    margin-top: 6vh;
  }
  .user-login {
    margin-top: 13vh;
  }
  .code {
    height: 5.5vh;
    position: relative;
  }
  .check-code {
    width: 30vw !important;
    text-align: top;
  }
  .get-code {
    /* display: inline-block; */
    background: #658de4;
    color: white;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    width: 30vw;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    letter-spacing: 2px;
  }
  .login-panel .login-btn {
    background: #658de4;
    color: white;
    border-radius: 20vw;
    width: 45%;
  }
  .login-footer {
    position: absolute;
    width: 100%;
    text-align: center;
    color: gray;
    bottom: 0;
    margin: 1vh !important;
    font-size: 12px;
    z-index: -1;
  }
  /* .input-item ::-webkit-input-placeholder {
      color: white;
    }
    .input-item ::-moz-input-placeholder {
      color: white;
    }
    .input-item ::-ms-input-placeholder {
      color: white;
    } */
}
</style>
